package com.zzti.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zzti.pojo.Employee;
import com.zzti.util.DButil;

public class EmployeeDao {
	// 查询全部员工的信息
	public List<Employee> queryEmployeeAll() {
		String sql = "select * from employee";
		ResultSet rs = DButil.select(sql, null);
		ArrayList<Employee> list = new ArrayList<Employee>();
		try {
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				int age = rs.getInt("age");
				String department = rs.getString("department");
				String type = rs.getString("type");
				int year = rs.getInt("year");
				Employee employee = new Employee(id, name, sex, age, department, type, year);
				list.add(employee);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DButil.closeAll();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	// 根据id查询某个员工信息
	public Employee selectById(Integer id) {
		String sql = "select * from employee where id = ?";
		ResultSet rs = DButil.select(sql, new Object[] { id });
		Employee employee = null;
		try {
			while (rs.next()) {
				int id1 = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				int age = rs.getInt("age");
				String department = rs.getString("department");
				String type = rs.getString("type");
				int year = rs.getInt("year");
				employee = new Employee(id1, name, sex, age, department, type, year);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return employee;
	}

	// 修改员工信息
	public int updateEmployeeById(Employee employee) {
		String sql = "update employee set name=?,sex=?,age=?,department=?,type=?,year=? where id=?";
		int i = DButil.update(sql, new Object[] { employee.getName(), employee.getSex(), employee.getAge(),
				employee.getDepartment(), employee.getType(), employee.getYear(), employee.getId() });
		return i;
	}

	// 删除员工信息
	public int deleteEmployeeById(int id) {
		String sql = "delete from employee where id = ?";
		int i = DButil.update(sql, new Object[] { id });
		return i;
	}

	// 增加员工信息
	public int addEmployee(Employee employee) {
		String sql = "insert into employee(name,sex,age,department,type,year) values(?,?,?,?,?,?)";
		int i = DButil.update(sql, new Object[] { employee.getName(), employee.getSex(), employee.getAge(),
				employee.getDepartment(), employee.getType(), employee.getYear() });
		return i;
	}
}
